/**
 * Created with IntelliJ IDEA.
 * Description:最大连续 1 的个数 1、最大连续 1 的个数 3
 * Date: 2024-02-17
 * Time:9:17
 */
public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int left = 0,right = 0;
        int count = 0;
        while (right < nums.length) {
            while(right < nums.length && nums[right] == 1) {  //进窗口
                count = Math.max(count,right-left+1);
                right++;
            }
            while(right < nums.length && nums[right] != 1) {  //出窗口
                right++;
            }
            left = right;
        }
        return count;
    }
    //////////////////////////////////////////////////
    public int longestOnes(int[] nums, int k) {
        int left = 0, right = 0;
        int zeroCount = 0;
        int res = 0;
        boolean isAllZero = true;
        for (; right < nums.length; right++) {
            if (nums[right] == 0) zeroCount++;
            else isAllZero = false;
            while (left < right && zeroCount > k) {
                if (nums[left] == 0) zeroCount--;
                left++;
            }
            res = Math.max(res, right - left + 1);
        }
        if (zeroCount <= k) isAllZero = false;
        if (isAllZero)
            return 0;
        return res;

    }
    ////////////////////////////////////////////////
    //二分查找
    public int search(int[] nums, int target) {
        int left = 0,right = nums.length;
        while(left <= right) {
            int mid = left+(right-left)/2;
            if(nums[mid] < target) left = mid+1;
            else if(nums[mid] > target) right = mid - 1;
            else return mid;
        }
        return -1;
    }
    /////////////////////////////////////////////////

}
